﻿using System;

namespace _06_Snake
{
    internal class Game
    {
        // 类 事物的抽象化概念
        // 如果想要使用 就需要实例化的对象
        // 类的默认访问权限是private

        public bool isOver;
        long currentTime, lastTime, fixTime;
        int fps;
        int speed;

        Menu menu;
        Snake snake;
        Map map;
        Food food;
        Coin coin;

        // 构造函数 类中的特殊方法
        // 在构造时（分配内存空间时 new）自动调用
        public Game(int width, int height)
        {
            //Console.WriteLine("Game的构造函数");
            menu = new Menu();
            map = new Map(width, height);
            food = new Food(width,height);
            snake = new Snake(width, height);
            coin = new Coin(width, height);
        }

        #region 初始化
        void Init()
        {
            Console.CursorVisible = false;
            //Console.WriteLine("Game初始化");
            isOver = false;
            currentTime =lastTime = 0;
            fps = 60;
            speed = 10;

            menu.Init();
            snake.Init(food, this);
            map.Init();
            food.Init(snake);
            coin.Init(snake);

            fixTime = DateTime.Now.Ticks;
        }
        #endregion

        #region 输入
        void Input()
        {
            //Console.WriteLine("Game输入");
            menu.Input();
            snake.Input();
            map.Input();
            food.Input();
        }
        #endregion

        #region 更新
        void Update()
        {
            //Console.WriteLine("Game更新");
            menu.Update();
            snake.Update();
            map.Update();
            food.Update();
            coin.Update();

            // 如果蛇头碰到了食物
            if (snake.pos[0].x == food.pos.x && snake.pos[0].y == food.pos.y)
            {
                // 则蛇生长一格 重新刷新食物
                snake.Grow();
                food.Spawn();
            }
        }

        void FixUpdate()
        {
            snake.FixUpdate();
        }

        #endregion

        #region 渲染
        void Render()
        {
            //Console.WriteLine("Game渲染");
            menu.Render();
            snake.Render();
            map.Render();
            food.Render();
            coin.Render();
        }
        #endregion

        #region 结束
        void End()
        {
            //Console.WriteLine("Game结束");
            menu.End();
            snake.End();
            map.End();
            food.End();
            coin.End();
        }
        #endregion

        #region 运行
        public void Play()
        {
            Init();

            lastTime = DateTime.Now.Ticks;
            fixTime = DateTime.Now.Ticks;
            while (!isOver)
            {
                Input();
                Update();
                currentTime = DateTime.Now.Ticks;
                if (currentTime - fixTime > 10_000_000 / speed)
                {
                    FixUpdate();
                    fixTime = DateTime.Now.Ticks;
                }
                if (currentTime - lastTime > 10_000_000 / fps)
                {
                    Render();
                    lastTime = DateTime.Now.Ticks;
                }
            }
            End();
        }
        #endregion
    }
}
